import 'package:boss/textStyle/text_style.dart';
import 'package:boss/ui/page/necessaryOrder/page/necessaryOrderDetail/provider/necessary_order_detail_provider.dart';
import 'package:boss/ui/widget/form/chip_form_field.dart';
import 'package:boss/ui/widget/form/base_text_form_field.dart';
import 'package:boss/ui/widget/radioList/model/radio_item_data.dart';
import 'package:boss/ui/widget/radioList/radio_list_picker.dart';
import 'package:boss/ui/widget/title_row_button.dart';

import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';

class NecessaryOrderDetailForm extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Expanded(
      child: SingleChildScrollView(
        padding: EdgeInsets.symmetric(
          horizontal: 30.w,
        ),
        child: Consumer<NecessaryOrderDetailProvider>(
          builder: (context, necessaryOrderDetailProvider, _) {
            return Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                BaseTextFormField(
                  prefixText: '名称',
                  isRequired: true,
                  hintText: '请输入名称',
                  initialValue: necessaryOrderDetailProvider.data.necessaryName ?? '',
                  onChanged: (value) {
                    necessaryOrderDetailProvider.setName(value);
                  },
                ),
                BaseTextFormField(
                  prefixText: '价格',
                  suffixText: '元',
                  hintText: '请输入',
                  keyboardType: TextInputType.number,
                  inputFormatters: [FilteringTextInputFormatter.allow(RegUtil.numberReg)],
                  initialValue: '${necessaryOrderDetailProvider.data.price == null ? '' : AmountUtil.price2String(necessaryOrderDetailProvider.data.price)}',
                  onChanged: (value) {
                    necessaryOrderDetailProvider.setPrice(value);
                  },
                ),
                ChipFormField(
                  title: '计费方式',
                  items: billMethods,
                  selectedItems: necessaryOrderDetailProvider.selectMethod,
                  onChanged: (selected, item) {
                    necessaryOrderDetailProvider.setMethod(item);
                  },
                ),
                ChipFormField(
                  title: '使用范围',
                  items: useRanges,
                  selectedItems: necessaryOrderDetailProvider.selectUseRange,
                  onChanged: (selected, item) {
                    necessaryOrderDetailProvider.setUseRange(item);
                  },
                ),
                BaseTextFormField(
                  prefixText: '数量',
                  suffixText: '份',
                  hintText: '请输入',
                  keyboardType: TextInputType.number,
                  inputFormatters: [FilteringTextInputFormatter.allow(RegUtil.integerReg)],
                  initialValue: '${necessaryOrderDetailProvider.data.everyoneCount ?? ''}',
                  onChanged: (value) {
                    necessaryOrderDetailProvider.setCount(value);
                  },
                ),
                TitleRowButton(
                  title: '使用桌台',
                  value: necessaryOrderDetailProvider.tableText,
                  hintText: '',
                  onTap: () async {
                    final tables = necessaryOrderDetailProvider.cacheTables;
                    final selected = necessaryOrderDetailProvider.data.useTableList;

                    var res = await showRadioListPicker<int>(
                      context,
                      title: '选择桌台',
                      items: tables
                          .map(
                            (item) => RadioItemData(
                              title: item.tableName,
                              value: item.tableId,
                            ),
                          )
                          .toList(),
                      value: selected.map((item) => item.tableId).toList(),
                      isMultiple: true,
                    );

                    if (res != null) {
                      necessaryOrderDetailProvider.setSelectTable(tables.where((item) => res.contains(item.tableId)).toList());
                    }
                  },
                ),
                Container(
                  margin: EdgeInsets.symmetric(
                    vertical: 17.w,
                  ),
                  child: Text(
                    '如不填价格默认为0元，如不填数量默认为1份',
                    style: TextStyleConstant().normal_4_22,
                  ),
                ),
              ],
            );
          },
        ),
      ),
    );
  }
}
